import requests
from lxml import etree

def pacho(url):
    response = requests.get(url)
    xml = etree.HTML(response.text)
    li = xml.xpath('//div[@class="mod newslist"]/ul/li/a/text()')
    text = ''.join(li)
    with open('腾讯社会新闻.txt','a',encoding='utf-8') as f:
        f.write(text)
url0 = 'https://news.qq.com/newssh/shwx/shehuiwanxiang.htm'
response = requests.get(url0)
xml = etree.HTML(response.text)
max_page = xml.xpath('//div[@class="pageNav"]/a[last()-1]/text()')[0]+''
print(f'总页数={max_page}')
for i in range(int(max_page)):
    if i == 0:
        url = url0
    else:
        url = url0[:-4] + '_%d.htm'%(i+1)
    pacho(url)
    print(f'第{i+1}页保存成功')
